﻿Ext.define("PSI.Accounting.AccountTitleAdmin.MainForm", {

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            // title: "码表结构管理",
            border: false,
            bodyBorder: false,
            layout: "border",
            items: [
                this.getAccountPanle(),
              //  this.basecodeAttrPanle()
            ]
        });

        me.panel = panel;
    },

    getMainPanel: function () {
        return this.panel;
    },

    getAccountPanle: function () {
        var me = this;
        var pan = Ext.create("Ext.panel.Panel", {
            id:'ff',
            region: "west",
            height: "100%",
            width: "100%",
            title: "科目维护",
            tbar: [
                { xtype: 'button', text: '新增科目', iconCls: "psi-button-add", handler: this.showAddAccount, scope: this },
                { xtype: 'button', text: '编辑科目', iconCls: "psi-button-edit", handler: this.editAccountTitle, scope: this },
                { xtype: 'button', text: '删除科目', iconCls: "psi-button-delete", handler:this.delAccountTitle,scope: this }
            ],
            layout: "fit",
            border: 1,
            items: [
               me.getTabPanle(),
            ]
        })

        return pan;
    },
    getTabPanle: function () {
        var me = this;
        var tab= Ext.create('Ext.tab.Panel', {
            width: "100%",
            region: "north",
            bodyStyle: 'border-width:0 0 0 0',
            renderTo: document.body,
            deferredRender: false,

            tabBar:{                
                defaults: {
                    width: 100
                }
            },
            //  border:0,
            listeners: {
                beforetabchange: function (tabs, newTab, oldTab) {
                    return  true;
                },
                tabchange:function( tabPanel, newCard, oldCard, eOpts )
                {
                        Ext.getCmp("detile").getForm().reset();
                        var tree = me.menuTree;
                        tree.getStore().load({ params: { atname: newCard.title } });
                        newCard.add(me.mytreedetilepanel);
                        newCard.add(me.mytreepanel)
                        newCard.doLayout();
                }
            },
            items: [{
                title: '资产',
                layout: 'border',
                items: [
                    me.getAccountTree(),
                    me.getAccountDetil()
                ]
  
            }, {
                title: '负债',
                layout: 'border',
            }, {
                title: "所有者权益",
                layout: 'border',
            }, {
                title: "成本",
                layout: 'border',
            }, {
                title: "损益",
                layout: 'border',
            }]
     
        });
        return tab
    },

    getAccountTree: function () {
        var me = this;

        var pan = Ext.create("Ext.panel.Panel", {
            region: "west",
            height: "100%",
            width: "25%",
            layout:"fit",
            border: false,
            items: [
              me.getTreeAT()
            ]
        })

        me.mytreepanel = pan;
        return pan;

    },

    getTreeAT: function () {
        var me=this;
        Ext.define("PSIMenuModel", {
            extend: "Ext.data.Model",
            fields: ["id", "text", "accountTitleName", "accountTitleCode", "accountTitleCategory", "isLeaf", "bizCategoryId", "expandable", "expanded", "leaf", "children", "fid"]
        });

        var MenuStore = Ext.create("Ext.data.TreeStore", {
            id: "treestore",
            model: "PSIMenuModel",
            proxy: {
                type: "ajax",
                extraParams: {atname:'资产'},
                actionMethods: { read: 'POST' },
                url: "/AccountTitleAdmin/GetATTree"
            }
        });

        var MenuTree = Ext.create('Ext.tree.Panel', {
            id: "accountTitletree",
            border: false,
            rootVisible: false,                 // 这个比较重要  隐藏根节点
            useArrows: true,                    //树结构显示风格
           // hideHeaders: true,                  //隐藏标题
            renderTo: Ext.getBody(),
            store: "treestore",
            columns: {
                items: [{
                    xtype: 'treecolumn',
                    dataIndex: 'accountTitleCode',
                    text:'科目码',
                    flex: 2
                }, {
                    dataIndex: 'accountTitleName',
                    text: '科目名称',
                    flex: 1
                }]
            },
            listeners: {

                itemdblclick: function (v, record, item, index, e){

                    var org = record.raw;

                    var form = Ext.create("PSI.Accounting.AccountTitleAdmin.AccountTitleEdit");

                    form.show(org);
                },
                select: function (model, records)
                {

                    Ext.getCmp("detile").getForm().loadRecord(records);
                }

            }


        });

        this.menuTree = MenuTree;

        return MenuTree;

    },

    getAccountDetil: function () {

        var me = this;
        var pan = Ext.create("Ext.form.Panel", {
            frame: true,
            id: 'detile',
            region: "east",
            height: "100%",
            width: "75%",
            title: "科目详情",
            bodyPadding:10,
            bodyStyle: 'border-width:0 0 0 1px',
            items: [
              {               
                  xtype: 'fieldset',
                  columnWidth: 1,
                  title: '科目信息',
                  collapsible: true,
                  defaultType: 'textfield',
                  defaults: { anchor: '20%' },
                  fieldDefaults: {
                      labelWidth: 70,
                      labelAlign: "right",
                      labelSeparator: "",
                      msgTarget: 'side'
                  },
                  layout: 'anchor',
                  items: [{
                      fieldLabel: '科目码',
                      name: 'accountTitleCode',

                  }, {
                      fieldLabel: '科目名称',
                      name: 'accountTitleName'
                  }, {
                      fieldLabel: '科目分类',
                      name: 'accountTitleCategory'
                  }, {
                      xtype:'checkbox',
                      fieldLabel: '末级科目',
                      name: 'isLeaf'
                  }, {
                      fieldLabel: '业务分类',
                      name: 'text'
                  }]
              }
             ]
        
        })

        me.mytreedetilepanel = pan;
        return pan;

    },

    showAddAccount: function () {

        var form = Ext.create("PSI.Accounting.AccountTitleAdmin.AccountTitleEdit");

        form.show();

    },

    delAccountTitle: function () {

        var me = this;
        var tree = this.menuTree;
        var item = tree.getSelectionModel().getSelection();
        if (item == null || item.length != 1) {
            PSI.MsgBox.showInfo("请选择要删除的科目");
            return;
        }

        var org = item[0].data;

        PSI.MsgBox.confirm("请确认是否删除科目",
        function () {
            Ext.getBody().mask("正在删除中...");
            Ext.Ajax.request({
                url: "/AccountTitleAdmin/DelAT",
                method: "POST",
                params: { ATId: org.id, ATCode: org.accountTitleCode },
                callback: function (options, success, response) {
                    Ext.getBody().unmask();

                    if (success) {
                        var data = Ext.JSON.decode(response.responseText);
                        if (data.success) {
                            PSI.MsgBox.showInfo("成功完成删除操作", function () {
                               
                                var store = tree.getStore();

                                store.load();

                            });
                        } else {
                            PSI.MsgBox.showInfo(data.msg);
                        }
                    } else {
                        PSI.MsgBox.showInfo("网络错误", function () {
                        });
                    }
                }

            });
        });

    },

    editAccountTitle:function(){
        var me = this;

        var tree = this.menuTree;
        var item = tree.getSelectionModel().getSelection();
        if (item == null || item.length != 1) {
            PSI.MsgBox.showInfo("请选择要编辑的科目");
            return;
        }

        var org = item[0].data;

        var form = Ext.create("PSI.Accounting.AccountTitleAdmin.AccountTitleEdit");

        form.show(org);
    
    }

})